<!DOCTYPE html>
<html  lang="en">
<head>
    <meta charset="utf-8">
<title>django内容总结 - noback</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />



    <meta name="description" content="django内容总结在后台对某些固定字段设置颜色# models.py from django.urls.html import format_html class Person(models.Model):     first_name &#x3D; models.CharField(max_length&#x3D;50)     last_name &#x3D; models.CharField(max_length&#x3D;50">
<meta property="og:type" content="article">
<meta property="og:title" content="django内容总结">
<meta property="og:url" content="http://alpaca-h.gitee.io/2019/12/29/blog_back_new/django/django%E5%86%85%E5%AE%B9%E6%80%BB%E7%BB%93/index.html">
<meta property="og:site_name" content="noback">
<meta property="og:description" content="django内容总结在后台对某些固定字段设置颜色# models.py from django.urls.html import format_html class Person(models.Model):     first_name &#x3D; models.CharField(max_length&#x3D;50)     last_name &#x3D; models.CharField(max_length&#x3D;50">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="http://alpaca-h.gitee.io/images/og_image.png">
<meta property="article:published_time" content="2019-12-29T12:40:44.000Z">
<meta property="article:modified_time" content="2019-12-29T12:56:13.070Z">
<meta property="article:author" content="Alpaca">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://alpaca-h.gitee.io/images/og_image.png">







<link rel="icon" href="/images/favicon.svg">


<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.7.2/css/bulma.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Ubuntu:400,600|Source+Code+Pro">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@9.12.0/styles/androidstudio.css">


    
    
    
    <style>body>.footer,body>.navbar,body>.section{opacity:0}</style>
    

    
    
    
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/lightgallery@1.6.8/dist/css/lightgallery.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/css/justifiedGallery.min.css">
    

    
    

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/outdatedbrowser@1.1.5/outdatedbrowser/outdatedbrowser.min.css">


    
    
    
    

<link rel="stylesheet" href="/css/back-to-top.css">


    
    

    
    
    
    

    
    
<link rel="stylesheet" href="/css/progressbar.css">
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

    
    
    

    
    
    
        <script async="" src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    

    


<link rel="stylesheet" href="/css/style.css">
<meta name="generator" content="Hexo 4.2.0"><link rel="alternate" href="/atom.xml" title="noback" type="application/atom+xml">
</head>
<body class="is-3-column">
    <nav class="navbar navbar-main">
    <div class="container">
        <div class="navbar-brand is-flex-center">
            <a class="navbar-item navbar-logo" href="/">
            
                <img src="/images/logo.svg" alt="django内容总结" height="28">
            
            </a>
        </div>
        <div class="navbar-menu">
            
            <div class="navbar-start">
                
                <a class="navbar-item"
                href="/">Home</a>
                
                <a class="navbar-item"
                href="/archives/">Archives</a>
                
                <a class="navbar-item"
                href="/categories/">Categories</a>
                
                <a class="navbar-item"
                href="/tags/">Tags</a>
                
                <a class="navbar-item"
                href="/about/">About</a>
                
            </div>
            
            <div class="navbar-end">
                
                    
                    
                    <a class="navbar-item" target="_blank" title="AlphaLxy GitHub" href="https://www.github.com/AlphaLxy">
                        
                        <i class="fab fa-github"></i>
                        
                    </a>
                    
                
                
                <a class="navbar-item is-hidden-tablet catalogue" title="Catalogue" href="javascript:;">
                    <i class="fas fa-list-ul"></i>
                </a>
                
                
                <a class="navbar-item search" title="Search" href="javascript:;">
                    <i class="fas fa-search"></i>
                </a>
                
            </div>
        </div>
    </div>
</nav>
    
    <section class="section">
        <div class="container">
            <div class="columns">
                <div class="column is-8-tablet is-8-desktop is-9-widescreen has-order-2 column-main"><div class="card">
    
    <div class="card-content article ">
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <i class="fas fa-angle-double-right"></i>django内容总结
            
        </h1>
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2019-12-29T12:40:44.000Z"><i class="far fa-calendar-alt">&nbsp;</i>2019-12-29</time>
                
                <time class="level-item has-text-grey is-hidden-mobile" datetime="2019-12-29T12:56:13.070Z"><i class="far fa-calendar-check">&nbsp;</i>2019-12-29</time>
                
                
                <div class="level-item">
                <i class="far fa-folder-open has-text-grey"></i>&nbsp;
                <a class="has-link-grey -link" href="/categories/blog-back-new/">blog_back_new</a>&nbsp;/&nbsp;<a class="has-link-grey -link" href="/categories/blog-back-new/django/">django</a>
                </div>
                
                
                <span class="level-item has-text-grey">
                    <i class="far fa-clock"></i>&nbsp;
                    
                    
                    13 minutes read (About 1914 words)
                </span>
                
                
                <span class="level-item has-text-grey" id="busuanzi_container_page_pv">
                    <i class="far fa-eye"></i>
                    <span id="busuanzi_value_page_pv">0</span> visits
                </span>
                
            </div>
        </div>
        
        <div class="content">
            <h1 id="django内容总结"><a href="#django内容总结" class="headerlink" title="django内容总结"></a>django内容总结</h1><h2 id="在后台对某些固定字段设置颜色"><a href="#在后台对某些固定字段设置颜色" class="headerlink" title="在后台对某些固定字段设置颜色"></a>在后台对某些固定字段设置颜色</h2><pre><code class="py"># models.py
from django.urls.html import format_html
class Person(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    color_code = models.CharField(max_length=6)


    def colored_name(self):

        return format_html(

            &#39;&lt;span style=&quot;color: #{};&quot;&gt;{} {}&lt;/span&gt;&#39;,

            self.color_code,

            self.first_name,

            self.last_name,

        )</code></pre>
<h3 id="admin-py"><a href="#admin-py" class="headerlink" title="admin.py"></a>admin.py</h3><p>class PersonAdmin(admin.ModelAdmin):<br>    list_display = (‘first_name’, ‘last_name’, ‘colored_name’)</p>
<h2 id="DjangoAdmin"><a href="#DjangoAdmin" class="headerlink" title="DjangoAdmin"></a>DjangoAdmin</h2><blockquote>
<p>Django自带后台管理模板是其一大特色，但作为后台管理很多地方需要我们去改进，当然也可以使用一些其他的后台模板</p>
</blockquote>
<ul>
<li>推荐模板1: xadmin xadmin在几年前是为django定制的后台管理模板，但是作者已经停止维护，但是在其分支中依旧有部分人在维护。<br>  具体使用跳转到我的掘金<a href="https://juejin.im/post/5afd267d51882542714ff756" target="_blank" rel="noopener">https://juejin.im/post/5afd267d51882542714ff756</a></li>
<li>推荐模板2： django simple-ui 在github上的星数并不高，但是是基于饿了么element-ui所写的django后台模板，我还未使用</li>
<li>使用本身自带的admin模板</li>
<li>使用其他兼容多语言的后台模板</li>
</ul>
<p>url :<a href="https://my.oschina.net/zhiyonghe/blog/1532030" target="_blank" rel="noopener">https://my.oschina.net/zhiyonghe/blog/1532030</a></p>
<hr>
<h3 id="自带的Admin模板"><a href="#自带的Admin模板" class="headerlink" title="自带的Admin模板"></a>自带的Admin模板</h3><blockquote>
<p>这里介绍一下自带的admin模板。</p>
</blockquote>
<ol>
<li>admin界面汉化<br>在setting.py文件中设置语言<pre><code class="python"># setting.py
</code></pre>
</li>
</ol>
<p>LANGUAGE_CODE = ‘zh-hans’<br>IME_ZONE = ‘Asia/Shanghai’</p>
<pre><code>
2. Model注册
在Model.py中创建完类后，需要在admin.py中完成注册，才能在后台模板中显示，方法有二

```python
# admin.py
# 方法一
from django.contrib import admin 
from blog.models import Blog

class BlogAdmin(admin.ModelAdmin):
    list_display=(&#39;xx&#39;,&#39;xx&#39;,&#39;xx&#39;)  # 要显示的字段
    list_per_page = 50  # 每页最多显示50条
    ordering = (&quot;-xxx&quot;)  # 设置排序，-表示降序排序
    list_editable = [&#39;&#39;,&#39;&#39;,&#39;&#39;] # 设置可编辑字段
    list_display_links = (&#39;&#39;,&#39;&#39;,) # 默认情况下只能点击第一个字段进入编辑，设置多个字段可点击进入编辑
    list_filter = (&#39;&#39;,&#39;&#39;,&#39;&#39;) # 筛选器
    search_fields  = (&#39;&#39;,&#39;&#39;,&#39;&#39;,) # 搜索字段
    date_hierarchy = &#39;go_time&#39; # 详细时间筛选

admin.site.register(Blog,BlogAdmin)

# 方法二
from djang.contrib import admin
from blog.models import Blog

@admin.register(Blog)
class BlogAdmin(admin.ModelAdmin):
    list_display=(&#39;&#39;,&#39;&#39;,&#39;&#39;)</code></pre><hr>
<h2 id="Django-for-ajax"><a href="#Django-for-ajax" class="headerlink" title="Django for ajax"></a>Django for ajax</h2><p>django 前后台传递消息内容的方法有很多种,这里我们来讲一下ajax for django</p>
<pre><code class="python"># 路由地址
# 项目路径下
path(&#39;&#39;,include(&#39;GraphPro.urls&#39;))
# 应用路径下
path(&#39;echartsdemo/&#39;,demoView.as_view(),name=&quot;demo&quot;),
# 视图层
class  demoView(View):
    def get(self,request):
        if request.GET.get(&#39;name&#39;) == &#39;hzj&#39;:
            return HttpResponse(&quot;ssdsdsd&quot;)
        else:
            print(2)
        return render(request,&#39;demo.html&#39;)
    def post(self,request):
        pass
# 模板层
&lt;body&gt;
    &lt;!-- 为ECharts准备一个具备大小（宽高）的Dom --&gt;
    &lt;div id=&quot;main&quot; style=&quot;width: 1000px;height:1000px;&quot; &gt;&lt;/div&gt;

    &lt;input type=&quot;text&quot; id=&quot;name&quot; value=&quot;hzj&quot;&gt;
    &lt;span id=&quot;result&quot;&gt;&lt;/span&gt;
    &lt;button class=&quot;button&quot; id=&quot;click_func&quot;&gt;点击&lt;/button&gt;
    {{ data }}


    &lt;script type=&quot;text/javascript&quot;&gt;

         $(document).ready(function () {
            var name = $(&#39;#name&#39;).val();
            var data = {&quot;name&quot;:name};
            $(&quot;#click_func&quot;).click(function () {
                $.get(
                    //请求的url
                    &#39;{% url 'demoecharts' %}&#39;,
                    data,
                    function (ret) {
                        console.log(ret)
                    }
                )
            })
        })


    &lt;/script&gt;
&lt;/body&gt;</code></pre>
<p>进入页面直接请求后台数据</p>
<pre><code class="html">
    &lt;script type=&quot;text/javascript&quot;&gt;

         $(document).ready(function () {
             var data = {&quot;name&quot;:&quot;hzj&quot;};
             $.get(
                //请求的url
                &#39;{% url 'demo' %}&#39;,
                data,
                function (ret) {
                    console.log(ret)
                }
             )
        })


    &lt;/script&gt;</code></pre>
<p>异步加载数据</p>
<pre><code class="html"> &lt;script type=&quot;text/javascript&quot;&gt;

         $(document).ready(function () {
             var data = {&quot;name&quot;:&quot;hzj&quot;};
             $.get(
                //请求的url
                &#39;{% url 'demo' %}&#39;,
                data,
             ).done(function(ret){
                 console.log(ret)
             })
        })


    &lt;/script&gt;
</code></pre>
<hr>
<blockquote>
<p>介绍Mysql在Django中的引用<br>由于Django在初始化项目后，所使用的数据库是sqlite3,但为了扩展，我们这里计划使用Mysql,配置步骤如下</p>
</blockquote>
<h2 id="配置"><a href="#配置" class="headerlink" title="配置"></a>配置</h2><pre><code class="python">1.在服务器上安装Mysql数据库(Centos版)

2.在django的setting.py中引用</code></pre>
<hr>
<h2 id="错误类型总结"><a href="#错误类型总结" class="headerlink" title="错误类型总结"></a>错误类型总结</h2><p>由于mysql与django在配置过程中会存在很多的错误，这里总结一下</p>
<h4 id="Mysql在Centos中权限不足的错误"><a href="#Mysql在Centos中权限不足的错误" class="headerlink" title="Mysql在Centos中权限不足的错误"></a>Mysql在Centos中权限不足的错误</h4><pre><code class="text">ERROR 2002 (HY000): Can&#39;t connect to local MySQL server through socket &#39;/var/lib/mysql/mysql.sock&#39; (2)</code></pre>
<p>原因是/var/lib/mysql的访问权限不足</p>
<pre><code class="text">1.改变权限 chown -R mysql:mysql /var/lib/mysql   
2. 启动服务器 /etc/init.d/mysql start (会自动生成mysql.sock文件)  
3.重新启动mysql服务  /etc/init.d/mysql start</code></pre>
<h4 id="密码类型错误"><a href="#密码类型错误" class="headerlink" title="密码类型错误"></a>密码类型错误</h4><pre><code class="text">1045, &quot;Access denied for user &#39;root&#39;@&#39;122.224.83.xxx  </code></pre>
<p>原因 密码错误，注意密码不能是字符串</p>
<pre><code class="text">GRANT ALL PRIVILEGES ON *.* TO root@&#39;%&#39; IDENTIFIED BY &#39;123456&#39; WITH GRANT OPTION;</code></pre>
<h4 id="django-setting-中的配置"><a href="#django-setting-中的配置" class="headerlink" title="django setting 中的配置"></a>django setting 中的配置</h4><pre><code class="text">DATABASES = {
    &#39;default&#39;: {

        &#39;ENGINE&#39;: &#39;django.db.backends.mysql&#39;,
        &#39;NAME&#39;: &#39;itemids&#39;,  # 数据库名字
        &#39;USER&#39;: &#39;root&#39;,    # 数据库登录名
        &#39;PASSWORD&#39;: &#39;12345x&#39;, # 数据库密码
        &#39;HOST&#39;: &#39;106.14.195.xxx&#39;, 数据库IP
        &#39;POST&#39;: 3306, # 端口
    }
}</code></pre>
<h4 id="引用pymysql"><a href="#引用pymysql" class="headerlink" title="引用pymysql"></a>引用pymysql</h4><p>由于MySQLdb只能使用在Python2中，在python3中已经停止了维护，所以这里我们引用pymyql的库</p>
<pre><code class="python">pipenv install pymysql
# 在与setting.py同级别的__init__.py中使用
import pymysql
pymysql.install_as_MySQLdb()</code></pre>
<h4 id="pymysql版本不符合"><a href="#pymysql版本不符合" class="headerlink" title="pymysql版本不符合"></a>pymysql版本不符合</h4><p>报错内容:django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.3 or newer is required; you have 0.7.11<br>修改Pipenv\Lib\site-packages\django\db\backends\mysql\base.py<br>注释下面内容<br>if version &lt; (1, 3, 3):<br>    raise ImproperlyConfigured(“mysqlclient 1.3.3 or newer is required; you have %s” % Database.<strong>version</strong>)</p>
<p>(在Centos中，pipenv install 所生成的包 会在pipenv shell 激活环境的时候出现)</p>
<p>由于上面注释，还会出现的报错内容为<br>File “C:\Users\Administrator\PycharmProjects\untitled1\venv\lib\site-packages\django\db\backends\mysql\operations.py”, line 146, in last_executed_query<br>将operations.py中的decode改成encode</p>
<h4 id="mysql不能插入中文"><a href="#mysql不能插入中文" class="headerlink" title="mysql不能插入中文"></a>mysql不能插入中文</h4><p>报错内容： django.db.utils.InternalError: (1366, “Incorrect string value: ‘\xE5\xAE\x9A\xE6\x97\xB6…’ for column ‘name’ at row 1”)<br>解决方法: <a href="https://blog.csdn.net/tzh476/article/details/52644271" target="_blank" rel="noopener">https://blog.csdn.net/tzh476/article/details/52644271</a><br>删除之前的库，创建一个新的数据库，使用utf8mb64   之前默认创建的都是latin1</p>
<h4 id="mysql-长度的问题"><a href="#mysql-长度的问题" class="headerlink" title="mysql 长度的问题"></a>mysql 长度的问题</h4><p>django.db.utils.InternalError: (1071, ‘Specified key was too long; max key length is 767 bytes’)<br><a href="https://blog.csdn.net/ljfphp/article/details/80406907" target="_blank" rel="noopener">https://blog.csdn.net/ljfphp/article/details/80406907</a><br><a href="https://www.orcode.com/question/407126_k280b8.html" target="_blank" rel="noopener">https://www.orcode.com/question/407126_k280b8.html</a></p>
<p>解决方法1: 升级Mysql5.6–&gt;&gt;Mysql5.7<br>centos7 下  对mysql的操作 <a href="https://blog.csdn.net/xufengzhu/article/details/81110982" target="_blank" rel="noopener">https://blog.csdn.net/xufengzhu/article/details/81110982</a><br><a href="https://blog.51cto.com/lisea/1941616" target="_blank" rel="noopener">https://blog.51cto.com/lisea/1941616</a></p>
<p>解决方法2 : 先使用utf8格式</p>
<p>—-&gt; 介绍Mysql在Django中的引用<br>由于Django在初始化项目后，所使用的数据库是sqlite3,但为了扩展，我们这里计划使用Mysql,配置步骤如下</p>
<h2 id="配置-1"><a href="#配置-1" class="headerlink" title="配置"></a>配置</h2><pre><code class="python">1.在服务器上安装Mysql数据库(Centos版)

2.在django的setting.py中引用</code></pre>
<hr>
<h2 id="错误类型总结-1"><a href="#错误类型总结-1" class="headerlink" title="错误类型总结"></a>错误类型总结</h2><p>由于mysql与django在配置过程中会存在很多的错误，这里总结一下</p>
<h4 id="Mysql在Centos中权限不足的错误-1"><a href="#Mysql在Centos中权限不足的错误-1" class="headerlink" title="Mysql在Centos中权限不足的错误"></a>Mysql在Centos中权限不足的错误</h4><pre><code class="text">ERROR 2002 (HY000): Can&#39;t connect to local MySQL server through socket &#39;/var/lib/mysql/mysql.sock&#39; (2)</code></pre>
<p>原因是/var/lib/mysql的访问权限不足</p>
<pre><code class="text">1.改变权限 chown -R mysql:mysql /var/lib/mysql   
2. 启动服务器 /etc/init.d/mysql start (会自动生成mysql.sock文件)  
3.重新启动mysql服务  /etc/init.d/mysql start</code></pre>
<h4 id="密码类型错误-1"><a href="#密码类型错误-1" class="headerlink" title="密码类型错误"></a>密码类型错误</h4><pre><code class="text">1045, &quot;Access denied for user &#39;root&#39;@&#39;122.224.83.xxx  </code></pre>
<p>原因 密码错误，注意密码不能是字符串</p>
<pre><code class="text">GRANT ALL PRIVILEGES ON *.* TO root@&#39;%&#39; IDENTIFIED BY &#39;123456&#39; WITH GRANT OPTION;</code></pre>
<h4 id="django-setting-中的配置-1"><a href="#django-setting-中的配置-1" class="headerlink" title="django setting 中的配置"></a>django setting 中的配置</h4><pre><code class="text">DATABASES = {
    &#39;default&#39;: {

        &#39;ENGINE&#39;: &#39;django.db.backends.mysql&#39;,
        &#39;NAME&#39;: &#39;itemids&#39;,  # 数据库名字
        &#39;USER&#39;: &#39;root&#39;,    # 数据库登录名
        &#39;PASSWORD&#39;: &#39;12345x&#39;, # 数据库密码
        &#39;HOST&#39;: &#39;106.14.195.xxx&#39;, 数据库IP
        &#39;POST&#39;: 3306, # 端口
    }
}</code></pre>
<h4 id="引用pymysql-1"><a href="#引用pymysql-1" class="headerlink" title="引用pymysql"></a>引用pymysql</h4><p>由于MySQLdb只能使用在Python2中，在python3中已经停止了维护，所以这里我们引用pymyql的库</p>
<pre><code class="python">pipenv install pymysql
# 在与setting.py同级别的__init__.py中使用
import pymysql
pymysql.install_as_MySQLdb()</code></pre>
<h4 id="pymysql版本不符合-1"><a href="#pymysql版本不符合-1" class="headerlink" title="pymysql版本不符合"></a>pymysql版本不符合</h4><p>报错内容:django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.3 or newer is required; you have 0.7.11<br>修改Pipenv\Lib\site-packages\django\db\backends\mysql\base.py<br>注释下面内容<br>if version &lt; (1, 3, 3):<br>    raise ImproperlyConfigured(“mysqlclient 1.3.3 or newer is required; you have %s” % Database.<strong>version</strong>)</p>
<p>(在Centos中，pipenv install 所生成的包 会在pipenv shell 激活环境的时候出现)</p>
<p>由于上面注释，还会出现的报错内容为<br>File “C:\Users\Administrator\PycharmProjects\untitled1\venv\lib\site-packages\django\db\backends\mysql\operations.py”, line 146, in last_executed_query<br>将operations.py中的decode改成encode</p>
<h4 id="mysql不能插入中文-1"><a href="#mysql不能插入中文-1" class="headerlink" title="mysql不能插入中文"></a>mysql不能插入中文</h4><p>报错内容： django.db.utils.InternalError: (1366, “Incorrect string value: ‘\xE5\xAE\x9A\xE6\x97\xB6…’ for column ‘name’ at row 1”)<br>解决方法: <a href="https://blog.csdn.net/tzh476/article/details/52644271" target="_blank" rel="noopener">https://blog.csdn.net/tzh476/article/details/52644271</a><br>删除之前的库，创建一个新的数据库，使用utf8mb64   之前默认创建的都是latin1</p>
<h4 id="mysql-长度的问题-1"><a href="#mysql-长度的问题-1" class="headerlink" title="mysql 长度的问题"></a>mysql 长度的问题</h4><p>django.db.utils.InternalError: (1071, ‘Specified key was too long; max key length is 767 bytes’)<br><a href="https://blog.csdn.net/ljfphp/article/details/80406907" target="_blank" rel="noopener">https://blog.csdn.net/ljfphp/article/details/80406907</a><br><a href="https://www.orcode.com/question/407126_k280b8.html" target="_blank" rel="noopener">https://www.orcode.com/question/407126_k280b8.html</a></p>
<p>解决方法1: 升级Mysql5.6–&gt;&gt;Mysql5.7<br>centos7 下  对mysql的操作 <a href="https://blog.csdn.net/xufengzhu/article/details/81110982" target="_blank" rel="noopener">https://blog.csdn.net/xufengzhu/article/details/81110982</a><br><a href="https://blog.51cto.com/lisea/1941616" target="_blank" rel="noopener">https://blog.51cto.com/lisea/1941616</a></p>
<p>解决方法2 : 先使用utf8格式</p>

        </div>
        
            <ul class="post-copyright">
            <li><strong>本文标题：</strong><a href="http://alpaca-h.gitee.io/2019/12/29/blog_back_new/django/django%E5%86%85%E5%AE%B9%E6%80%BB%E7%BB%93/">django内容总结</a></li>
            <li><strong>本文作者：</strong><a href="http://alpaca-h.gitee.io">Alpaca</a></li>
            <li><strong>本文链接：</strong><a href="http://alpaca-h.gitee.io/2019/12/29/blog_back_new/django/django%E5%86%85%E5%AE%B9%E6%80%BB%E7%BB%93/">http://alpaca-h.gitee.io/2019/12/29/blog_back_new/django/django%E5%86%85%E5%AE%B9%E6%80%BB%E7%BB%93/</a></li>
            <li><strong>发布时间：</strong>2019-12-29</li>
            <li><strong>版权声明：</strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" rel="external nofollow" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明出处！
            </li>
            </ul>
        
        
        
        
    </div>
</div>





<div class="card card-transparent">
    <div class="level post-navigation is-flex-wrap is-mobile">
        
        <div class="level-start">
            <a class="level level-item has-link-grey  article-nav-prev" href="/2019/12/29/blog_back_new/django/django%E5%AD%97%E6%AE%B5%E6%80%BB%E7%BB%93/">
                <i class="level-item fas fa-chevron-left"></i>
                <span class="level-item">django字段总结</span>
            </a>
        </div>
        
        
        <div class="level-end">
            <a class="level level-item has-link-grey  article-nav-next" href="/2019/12/29/blog_back_new/django/django%E9%94%99%E8%AF%AF%E5%90%88%E9%9B%86/">
                <span class="level-item">django错误合集</span>
                <i class="level-item fas fa-chevron-right"></i>
            </a>
        </div>
        
    </div>
</div>



</div>
                




<div class="column is-4-tablet is-4-desktop is-3-widescreen  has-order-1 column-left ">
    
        
<div class="card widget">
    <div class="card-content">
        <nav class="level" style="margin-bottom:1rem">
            <div class="level-item has-text-centered">
                <div>
                    
                        <img class="image is-96x96 has-mb-6" src="https://www.gravatar.com/avatar/e0f4032c0f2d1068ffffbaf93c0bef52?s=96" alt="Xinyu Liu">
                    
                    
                    <p class="is-size-4 is-block">
                        Xinyu Liu
                    </p>
                    
                    
                    <p class="is-size-6 is-block">
                        Alpha Lxy
                    </p>
                    
                    
                    <p class="is-size-6 is-flex is-flex-center has-text-grey">
                        <i class="fas fa-map-marker-alt has-mr-7"></i>
                        <span>Beijing, China</span>
                    </p>
                    
                </div>
            </div>
        </nav>
        <nav class="level menu-list is-mobile" style="margin-bottom:1rem">
            <div class="level-item has-text-centered is-marginless">
                <a href="/archives/">
                    <p class="heading">
                        Posts
                    </p>
                    <p class="title has-text-weight-normal">
                        40
                    </p>
                </a>
            </div>
            <div class="level-item has-text-centered is-marginless">
                <a href="/categories/">
                    <p class="heading">
                        Categories
                    </p>
                    <p class="title has-text-weight-normal">
                        13
                    </p>
                </a>
            </div>
            <div class="level-item has-text-centered is-marginless">
                <a href="/tags/">
                    <p class="heading">
                        Tags
                    </p>
                    <p class="title has-text-weight-normal">
                        0
                    </p>
                </a>
            </div>
        </nav>
        <div class="level">
            <a class="level-item button is-link is-rounded" href="https://www.github.com/AlphaLxy" target="_blank">
                <i class="fab fa-github"></i>&nbsp;&nbsp;Follow</a>
        </div>
        
        
    </div>
</div>

    
        
<div class="card widget column-left is-sticky" id="toc">
    <div class="card-content">
        <div class="menu">
            <h3 class="menu-label">
                Catalogue
            </h3>
            <ul class="menu-list"><li>
        <a class="is-flex" href="#django内容总结">
        <span class="has-mr-6">1</span>
        <span>django内容总结</span>
        </a><ul class="menu-list"><li>
        <a class="is-flex" href="#在后台对某些固定字段设置颜色">
        <span class="has-mr-6">1.1</span>
        <span>在后台对某些固定字段设置颜色</span>
        </a><ul class="menu-list"><li>
        <a class="is-flex" href="#admin-py">
        <span class="has-mr-6">1.1.1</span>
        <span>admin.py</span>
        </a></li></ul></li><li>
        <a class="is-flex" href="#DjangoAdmin">
        <span class="has-mr-6">1.2</span>
        <span>DjangoAdmin</span>
        </a><ul class="menu-list"><li>
        <a class="is-flex" href="#自带的Admin模板">
        <span class="has-mr-6">1.2.1</span>
        <span>自带的Admin模板</span>
        </a></li></ul></li><li>
        <a class="is-flex" href="#Django-for-ajax">
        <span class="has-mr-6">1.3</span>
        <span>Django for ajax</span>
        </a></li><li>
        <a class="is-flex" href="#配置">
        <span class="has-mr-6">1.4</span>
        <span>配置</span>
        </a></li><li>
        <a class="is-flex" href="#错误类型总结">
        <span class="has-mr-6">1.5</span>
        <span>错误类型总结</span>
        </a></li><li>
        <a class="is-flex" href="#配置-1">
        <span class="has-mr-6">1.6</span>
        <span>配置</span>
        </a></li><li>
        <a class="is-flex" href="#错误类型总结-1">
        <span class="has-mr-6">1.7</span>
        <span>错误类型总结</span>
        </a></li></ul></li></ul>
        </div>
    </div>
</div>


    
    
        <div class="column-right-shadow is-hidden-widescreen ">
        
        </div>
    
</div>

                
            </div>
        </div>
    </section>
    <footer class="footer">
    <div class="container">
        <div class="level">
            <div class="level-start has-text-centered-mobile">
                <a class="footer-logo is-block has-mb-6" href="/">
                
                    <img src="/images/logo.svg" alt="django内容总结" height="28">
                
                </a>
                <p class="is-size-7">
                &copy; 2020 Alpaca&nbsp;
                Powered by <a href="http://hexo.io/" target="_blank">Hexo</a> & <a
                        href="http://github.com/ppoffice/hexo-theme-icarus" target="_blank">Icarus</a>
                
                <br>
                <span id="busuanzi_container_site_uv">
                Visited by <span id="busuanzi_value_site_uv">0</span> users
                </span>
                
                </p>
            </div>
            <div class="level-end">
            
                <div class="field has-addons is-flex-center-mobile has-mt-5-mobile is-flex-wrap is-flex-middle">
                
                
                <p class="control">
                    <a class="button is-white is-large" target="_blank" title="CC BY-NC-SA 4.0" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
                        
                        <i class="fab fa-creative-commons"></i>&nbsp;<i class="fab fa-creative-commons-by"></i>&nbsp;<i class="fab fa-creative-commons-nc"></i>&nbsp;<i class="fab fa-creative-commons-sa"></i>&nbsp;
                        
                    </a>
                </p>
                
                <p class="control">
                    <a class="button is-white is-large" target="_blank" title="AlphaLxy GitHub" href="https://www.github.com/AlphaLxy">
                        
                        <i class="fab fa-github"></i>&nbsp;
                        
                    </a>
                </p>
                
                </div>
            
            </div>
        </div>
    </div>
</footer>

    <script src="https://cdn.jsdelivr.net/npm/jquery@3.3.1/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/moment@2.22.2/min/moment-with-locales.min.js"></script>
<script>moment.locale("en");</script>


    
    
    
    <script src="/js/animation.js"></script>
    

    
    
    
    <script src="https://cdn.jsdelivr.net/npm/lightgallery@1.6.8/dist/js/lightgallery.min.js" defer></script>
    <script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js" defer></script>
    <script src="/js/gallery.js" defer></script>
    

    
    

<div id="outdated">
    <h6>Your browser is out-of-date!</h6>
    <p>Update your browser to view this website correctly. <a id="btnUpdateBrowser" href="http://outdatedbrowser.com/" target="_blank" rel="noopener">Update
            my browser now </a></p>
    <p class="last"><a href="#" id="btnCloseUpdateBrowser" title="Close">&times;</a></p>
</div>
<script src="https://cdn.jsdelivr.net/npm/outdatedbrowser@1.1.5/outdatedbrowser/outdatedbrowser.min.js" defer></script>
<script>
    document.addEventListener("DOMContentLoaded", function () {
        outdatedBrowser({
            bgColor: '#f25648',
            color: '#ffffff',
            lowerThan: 'flex'
        });
    });
</script>


    
    
<script src="https://cdn.jsdelivr.net/npm/mathjax@2.7.5/unpacked/MathJax.js?config=TeX-MML-AM_CHTML" defer></script>
<script>
document.addEventListener('DOMContentLoaded', function () {
    MathJax.Hub.Config({
        'HTML-CSS': {
            matchFontHeight: false
        },
        SVG: {
            matchFontHeight: false
        },
        CommonHTML: {
            matchFontHeight: false
        },
        tex2jax: {
            inlineMath: [
                ['$','$'],
                ['\\(','\\)']
            ]
        }
    });
});
</script>

    
    

<a id="back-to-top" title="Back to Top" href="javascript:;">
    <i class="fas fa-chevron-up"></i>
</a>
<script src="/js/back-to-top.js" defer></script>


    
    

    
    
    
    

    
    
    
    
    
    <script src="https://cdn.jsdelivr.net/npm/clipboard@2.0.4/dist/clipboard.min.js" defer></script>
    <script src="/js/clipboard.js" defer></script>
    

    
    
    

    


<script src="/js/main.js" defer></script>

    
    <div class="searchbox ins-search">
    <div class="searchbox-container ins-search-container">
        <div class="searchbox-input-wrapper">
            <input type="text" class="searchbox-input ins-search-input" placeholder="Type something..." />
            <span class="searchbox-close ins-close ins-selectable"><i class="fa fa-times-circle"></i></span>
        </div>
        <div class="searchbox-result-wrapper ins-section-wrapper">
            <div class="ins-section-container"></div>
        </div>
    </div>
</div>
<script>
    (function (window) {
        var INSIGHT_CONFIG = {
            TRANSLATION: {
                POSTS: 'Posts',
                PAGES: 'Pages',
                CATEGORIES: 'Categories',
                TAGS: 'Tags',
                UNTITLED: '(Untitled)',
            },
            CONTENT_URL: '/content.json',
        };
        window.INSIGHT_CONFIG = INSIGHT_CONFIG;
    })(window);
</script>
<script src="/js/insight.js" defer></script>
<link rel="stylesheet" href="/css/search.css">
<link rel="stylesheet" href="/css/insight.css">
    
</body>
</html>